package com.future.biz;

import java.lang.reflect.InvocationTargetException;

import com.future.model.bo.RegisterBO;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.dao.AreaDAO;
import com.future.dao.DepartmentDAO;
import com.future.dao.UserDAO;
import com.future.model.bo.UserBO;
import com.future.model.po.AreaModel;
import com.future.model.po.DepartmentModel;
import com.future.model.po.UserModel;

public class LoginBIZ {

	private static Logger logger = LogManager.getLogger(LoginBIZ.class);

	private static Integer USER_LEVEL_PROVINCE = 1; // 省级
	private static Integer USER_LEVEL_CITY = 2; // 市级
	private static Integer USER_LEVEL_COUNTY = 3; // 县级
	private static Integer USER_LEVEL_TOWN = 4; // 街乡、社区级
	private static Integer USER_LEVEL_COMMUNITY = 5; // 街乡、社区级

	private static Integer SURVEY_TYPE_LOCAL = 1; // 地方调查
	private static Integer SURVEY_TYPE_DEPARTMENT = 2; // 部门调查
	private static Integer SURVEY_TYPE_TOWN = 3; // 街乡调查
	private static Integer COMMUNITY=4;//社区调查

	private UserBO userBo = new UserBO();

	public UserBO login(String username,
											String password) throws Exception {
		DepartmentDAO departmentDao = new DepartmentDAO();
		AreaDAO areaDao = new AreaDAO();
		DepartmentModel departmentModel;
		AreaModel areaModel;
		UserModel userModel = loginCheck(username, password);
		if (userModel == null) {
			throw new Exception("登录失败");
		}
		userBo.setUser(userModel);
		departmentModel = departmentDao.findById(userModel.getDepartmentId());
		userBo.setDepartment(departmentModel);
		areaModel = areaDao.findById(userModel.getAreaId());
		userBo.setArea(areaModel);
		int level = generateUserLevel(areaModel.getCode());
		userBo.setUserLevel(level);
		int surveyType = this.generateSurveyType(userModel.getDepartmentId());
		userBo.setSurveyType(surveyType);
		return userBo;
	}
	public UserBO login(String username) throws Exception {
		DepartmentDAO departmentDao = new DepartmentDAO();
		AreaDAO areaDao = new AreaDAO();
		DepartmentModel departmentModel;
		AreaModel areaModel;
		UserModel userModel = loginCheck(username);
		if (userModel == null) {
			throw new Exception("该地区未注册");
		}
		userBo.setUser(userModel);
		departmentModel = departmentDao.findById(userModel.getDepartmentId());
		userBo.setDepartment(departmentModel);
		areaModel = areaDao.findById(userModel.getAreaId());
		userBo.setArea(areaModel);
		int level = generateUserLevel(areaModel.getCode());
		userBo.setUserLevel(level);
		int surveyType = this.generateSurveyType(userModel.getDepartmentId());
		userBo.setSurveyType(surveyType);
		return userBo;
	}

	public UserBO regist(RegisterBO registerBo) throws Exception {
		boolean repeatFlag = registerRepeatCheck(registerBo);
		boolean passwordFlag = registCheckPassword(registerBo);
		UserModel userModel = new UserModel();
		if (repeatFlag && passwordFlag) {
			logger.debug("验证通过");
			try {
				BeanUtils.copyProperties(userModel, registerBo);
				UserDAO userDao = new UserDAO();
				AreaDAO areaDao = new AreaDAO();
//				AreaModel area = areaDao.findByCode(userModel.getAreaId().toString()).get(0);
				AreaModel area = areaDao.findByCode(registerBo.getAreaId()).get(0);
				userModel.setDepartmentId(Integer.valueOf(userModel.getDepartmentId()));
				userModel.setAreaId(area.getId());
				userDao.saveUser(userModel);
				return this.login(userModel.getUsername(), userModel.getPassword());
			} catch (IllegalAccessException e) {
				logger.error(e);
			} catch (InvocationTargetException e) {
				logger.error(e);
			}
		} else {
			logger.debug("验证失败");
			throw new Exception("用户重名或输入密码错误");
		}
		return null;
	}

	private boolean registerRepeatCheck(RegisterBO registerBo) {
		boolean isRepeat = false;
		UserDAO userDao = new UserDAO();
		try {
			userDao.findByUsername(registerBo.getUsername());
		} catch (Exception e) {
			logger.error(e);
			isRepeat = true;
		}
		return !isRepeat;
	}

	private boolean registCheckPassword(RegisterBO registerBo) {

		return registerBo.getPassword().equals(registerBo.getPasswordConfirm());
	}

	private UserModel loginCheck(	String username,String password) {
		UserDAO userDao = new UserDAO();
		UserModel userModel = new UserModel();
		try {
			userModel = userDao.findUser(username, password);
		} catch (Exception e) {
			logger.error(e);
			userModel = null;
		}
		return userModel;
	}
	private UserModel loginCheck(String username) {
		UserDAO userDao = new UserDAO();
		UserModel userModel = new UserModel();
		try {
			userModel = userDao.findUser(username);
		} catch (Exception e) {
			logger.error(e);
			userModel = null;
		}
		return userModel;
	}

	private int generateUserLevel(String areaCode) {
		int provinceFlag = areaCode.lastIndexOf("0000000000");
		int cityFlag = areaCode.lastIndexOf("00000000");
		int countyFlag = areaCode.lastIndexOf("000000");
		int commFlag = areaCode.lastIndexOf("000");
		if (provinceFlag > 0) {
			return USER_LEVEL_PROVINCE;
		} else if (cityFlag > 0) {
			return USER_LEVEL_CITY;
		} else if (countyFlag > 0) {
			return USER_LEVEL_COUNTY;
		} else if (commFlag > 0) {
			return USER_LEVEL_TOWN;
		} else {
			return USER_LEVEL_COMMUNITY;
		}
	}

	private int generateSurveyType(Integer departmentId) {
		switch (departmentId) {
		case 1:
			return SURVEY_TYPE_LOCAL;
		case 34:
			return SURVEY_TYPE_TOWN;
		case 35:
			return COMMUNITY;
		default:
			return SURVEY_TYPE_DEPARTMENT;
		}
	}
}
